Skip to content

使用容器构建dotnet AOT

发布于:

Host环境

  • 操作系统: Ubuntu 22.04.4 LTS
  • 容器环境: Docker Engine - Community 27.0.3
  • 镜像: mcr.microsoft.com/dotnet/sdk 8.0 6c35d18d4390

准备

命令

简易版

bash
docker run \
-it --rm \
--name dotnet-builder \
-v $(pwd):/src \
-w /src \
mcr.microsoft.com/dotnet/sdk:8.0 \
bash -c "sed -i 's/http:\/\/deb.debian.org/https:\/\/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && apt update && apt install -y clang zlib1g-dev && dotnet publish -c Release -r linux-x64 --no-self-contained"

完整版

  1. 创建容器
bash
docker run \
-it --rm \
--name dotnet-builder \
-v $(pwd):/src \
-w /src \
mcr.microsoft.com/dotnet/sdk:8.0 \
bash
  1. 配置debian软件源镜像
bash
mv /etc/apt/sources.list.d/debian.sources /etc/apt/sources.list.d/debian.sources.bak
cat << EOF > /etc/apt/sources.list.d/debian.sources
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/debian
Suites: bookworm bookworm-updates bookworm-backports
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
# Types: deb-src
# URIs: https://mirrors.tuna.tsinghua.edu.cn/debian
# Suites: bookworm bookworm-updates bookworm-backports
# Components: main contrib non-free non-free-firmware
# Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
Types: deb
URIs: https://security.debian.org/debian-security
Suites: bookworm-security
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

# Types: deb-src
# URIs: https://security.debian.org/debian-security
# Suites: bookworm-security
# Components: main contrib non-free non-free-firmware
# Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
EOF
apt update
apt install -y clang zlib1g-dev
  1. 编译/发布
bash
dotnet publish -c Release -r linux-x64 --no-self-contained

终极版, 制作容器镜像

  1. 创建Dockerfile
shell
cat << EOF > Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:8.0
RUN sed -i 's/http:\/\/deb.debian.org/https:\/\/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \
    apt update && \
    apt install -y clang zlib1g-dev
EOF
docker build -t dotnet/sdk-aot:8.0 .
  1. 构建
bash
docker run \
-it --rm \
--name dotnet-builder \
-v $(pwd):/src \
-w /src \
dotnet/sdk-aot:8.0 \
dotnet publish -c Release -r linux-x64 --no-self-contained